﻿using MySql.Data.MySqlClient;
using SeCompUFSCarWeb.Evento;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SeCompUFSCarWeb.Administration
{
    public partial class Cobranca : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnEnviar_Click(object sender, EventArgs e)
        {

            string strconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            MySqlConnection conn = new MySqlConnection(strconn);

            string query = "SELECT * FROM secomp.cobranca";
            MySqlCommand cmd = new MySqlCommand(query, conn);

            string pacote = "";
            string htmlCode = "";

            try
            {
                conn.Open();
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    switch (reader.GetInt32("pack"))
	                {
                        case 1:
                            pacote = "Hunger";
                            break;
                        case 2:
                            pacote = "Study";
                            break;
                        case 3:
                            pacote = "Full";
                            break;
		                default:
                            pacote = "";
                            break;
	                }

                    htmlCode = System.IO.File.ReadAllText(Server.MapPath("~/Emails/Cobranca.html"));
                    htmlCode = htmlCode.Replace("#NOME", reader.GetString("nome"))
                                        .Replace("#PACOTE", pacote)
                                        .Replace("#NUMERO", reader.GetInt32("id").ToString())
                                        .Replace("#VALOR", reader.GetInt32("total").ToString());

                    Mail.Send(reader.GetString("email"), htmlCode, "V SeComp: Ainda dá tempo!");
                }
            }
            catch (Exception ex)
            {
                Message.Text = "Affz deu merda aqui véi, se liga: " + ex.ToString();
            }
            finally
            {
                conn.Close();
            }

            Message.Text = "Emails enviados com sucesso.";
        }
    }
}